package cn.koala.key.stock.service.impl;

import cn.koala.key.api.KeyManagementApi;
import cn.koala.key.common.feign.FeignUtils;
import cn.koala.key.common.utils.DateUtils;
import cn.koala.key.entity.KeyInfo;
import cn.koala.key.entity.enums.InventoryEnum;
import cn.koala.key.stock.domain.dto.InventorySearch;
import cn.koala.key.stock.domain.vo.KeyInfoExcel;
import cn.koala.key.stock.domain.vo.KeyInfoVO;
import cn.koala.key.stock.domain.vo.PrintBarCodeVO;
import cn.koala.key.stock.service.KeyInventoryService;
import cn.seed.common.core.Result;
import cn.seed.common.core.ResultList;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 库存逻辑
 * @description
 * @author shuai.xu
 * @time 2019/5/6 9:51
*/
@Service
public class KeyInventoryServiceImpl implements KeyInventoryService {


    @Override
    public Result<ResultList<KeyInfoVO>> list(InventorySearch inventorySearch) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).selectInventory(inventorySearch);
    }

    @Override
    public Result<List<KeyInfoVO>> exportInventory(InventorySearch inventorySearch) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).exportInventory(inventorySearch);
    }

    @Override
    public List<KeyInfoExcel> dataProcessing(List<KeyInfoVO> data) {
        List<KeyInfoExcel> resultList = new ArrayList<>();
        data.forEach(keyInfoVO -> {
            KeyInfoExcel keyInfoExcel = new KeyInfoExcel();
            keyInfoExcel.setAddTime(DateUtils.getDay(keyInfoVO.getAddTime()));
            keyInfoExcel.setBrandSeriesName(keyInfoVO.getBrandName()+keyInfoVO.getSeriesName());
            keyInfoExcel.setCarNo(keyInfoVO.getCarNo());
            keyInfoExcel.setCarVin(keyInfoVO.getCarVin());
            keyInfoExcel.setKeyBarCode(keyInfoVO.getKeyBarCode());
            keyInfoExcel.setWarehouseLocation(keyInfoVO.getWarehouseLocation());
            keyInfoExcel.setDropReason(keyInfoVO.getDropReason());
            if (InventoryEnum.BORROW.getValue().equals(keyInfoVO.getKeyStatus())){
                keyInfoExcel.setKeyStatus(InventoryEnum.BORROW.getName());
            }else if(InventoryEnum.IN_STOCK.getValue().equals(keyInfoVO.getKeyStatus())){
                keyInfoExcel.setKeyStatus(InventoryEnum.IN_STOCK.getName());
            }else if(InventoryEnum.REPORT_LOSS.getValue().equals(keyInfoVO.getKeyStatus())){
                keyInfoExcel.setKeyStatus(InventoryEnum.REPORT_LOSS.getName());
            }
            resultList.add(keyInfoExcel);
        });
        return resultList;
    }

    @Override
    public Result<PrintBarCodeVO> printBarCode(String keyBarCode) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).printBarCode(keyBarCode);
    }

    @Override
    public Result<List<PrintBarCodeVO>> batchPrintBarCode(Integer startCode, Integer endCode) {
        return FeignUtils.getHttpApi(KeyManagementApi.class).batchPrintBarCode(startCode,endCode);
    }
}
